/**
 * Tab相关变量与方法
 */
var OPENED_TABS_ID = [];
var ORIGINAL_TAB_ID = null;
var ACTIVE_TAB_ID = null;
var IS_DIRTY_FUNC = {};
Array.prototype.indexOf = function(o) {
	for (var i = 0; i < this.length; i++)
		if (this[i] == o)
			return i;
	return -1;
}

Array.prototype.contains = function(o) {
	for (var i = 0; i < this.length; i++)
		if (this[i] == o)
			return true;
	return false;
}

Array.prototype.del = function(n) {
	if (n < 0)
		return this;
	return this.slice(0, n).concat(this.slice(n + 1, this.length));
}
String.prototype.sub = function(n) {
	var r = /[^\x00-\xff]/g;
	if (this.replace(r, "mm").length <= n)
		return this;
	var m = Math.floor(n / 2);
	for (var i = m; i < this.length; i++) {
		if (this.substr(0, i).replace(r, "mm").length >= n) {
			return this.substr(0, i);
		}
	}
	return this;
};
String.prototype.len = function() {
	return this.replace(/[^\x00-\xff]/g, "**").length;
};
var TabTagUtils = {
	tabQueue : [],
	pageLoadingFlag : false,
	pageLoadingTimeOutObj : null,
	pageLoadingIntervalObj : null,
	openPageLoading : function(){
		if(!TabTagUtils.pageLoadingFlag){
			TB_showMaskLayer(i18n.nav.tabLoading);
			TabTagUtils.pageLoadingFlag = true;
		}
	},
	removePageLoading : function(menuId){
		//1.获取正在加载中页面的<div id="pageLoadComplete" isComplete="false" style="display: none;"></div>
		var divPageLoadComplete = null;
		//判断浏览器
		if($.browser.msie){		
			divPageLoadComplete = $(window.parent.document.frames[menuId+"_frame"].document).find("div[id='pageLoadComplete']");
		}else{
			divPageLoadComplete = $(window.parent.document.getElementById(menuId+"_frame").contentWindow.document).find("div[id='pageLoadComplete']");
		}
    	if(divPageLoadComplete.length > 0){
    		//alert(1);
    		//2.开启时间轮询对象,判断正在加载的页面是否 全部加载完成 
    		TabTagUtils.pageLoadingIntervalObj = window.setInterval(function(){
    			//3.divPageLoadComplete 的属性complete=true 时,表示页面加载完成
    	    	if(divPageLoadComplete.attr('isComplete')=="true" || divPageLoadComplete.attr('isComplete')===true){
    	    		//alert(1.1);
    	    		//4.关闭 页面加载等待框
    	    		if(TabTagUtils.pageLoadingFlag){
    	    			//alert(1.11);
    	    			//4.1.移除等待框
    	    			TB_removeMaskLayer();
    	    			//4.2回收timeout时间对象
    	    			window.clearTimeout(TabTagUtils.pageLoadingTimeOutObj);
    	    			TabTagUtils.pageLoadingFlag = false;
    	    		}
    	    		//5.回收时间轮询对象
    	    		window.clearInterval(TabTagUtils.pageLoadingIntervalObj);
    	    		//alert(1.2);
    	    	}
    	    },300);
    	}
    	//2.正在加载中页面，如果没有<div id="pageLoadComplete" isComplete="false" style="display: none;"></div>
    	else{
    		//alert(2);
    		if(TabTagUtils.pageLoadingFlag){
    			//alert(2.1);
    			//2.1.移除等待框
    			TB_removeMaskLayer();
    			//2.2回收timeout时间对象
    			window.clearTimeout(TabTagUtils.pageLoadingTimeOutObj);
    			//alert(2.3);
    			TabTagUtils.pageLoadingFlag = false;
    		}
    	}
	},
	newTab : function(menuId, menuName, menuURL, closeAble, validate) {
		if (OPENED_TABS_ID.contains(menuId)) {
			var event = {
				data : {
					tabId : menuId
				}
			};
			TabTagUtils.showTab(event);
			return;
		}
		
		if (OPENED_TABS_ID.length >= ProjConfigParam.tabCount) {
			var msgTooManyTbs = '';			
				msgTooManyTbs = i18n.tab.msgTooManyTabs;			
				alert(msgTooManyTbs);
			return 'TABOVER';
		}
		TabTagUtils.openPageLoading();
		TabTagUtils.openTab(menuId, menuName, menuURL, closeAble, validate);
		TabTagUtils.pageLoadingTimeOutObj = window.setTimeout('TabTagUtils.removePageLoading("'+menuId+'")',ProjConfigParam.tabLoadingTime);
	},
	openTab : function(menuId, menuName, menuURL, closeAble, validate){
		
		var oDivTab = document.getElementById('divTab');
		var divNewTab = document.createElement('div');
		divNewTab.id = menuId;
		$(divNewTab).attr('class', 'tab-title-up ui-state-active');
		$(divNewTab).attr('style', 'display:block;');

		var divLeft = document.createElement('div');
		divLeft.innerHTML = '&nbsp;';
		$(divLeft).attr('class', 'tab-title-left');

		var divCenter = document.createElement('div');
		var shortMenuName = menuName.sub(8);
		if (shortMenuName != menuName) {
			shortMenuName += '...';
		}
		divCenter.innerHTML = shortMenuName;
		$(divCenter).attr('class', 'tab-title-center');
		$(divCenter).attr('title', menuName);
		$(divCenter).bind('click', {
					tabId : menuId
				}, TabTagUtils.showTab);

		var divRight = document.createElement('div');
		divRight.innerHTML = '&nbsp;';
		$(divRight).attr('class', 'tab-title-right');

		divNewTab.appendChild(divLeft);
		divNewTab.appendChild(divCenter);
		divNewTab.appendChild(divRight);

		if (!(closeAble == false)) {
			var divClose = document.createElement('div');
			$(divClose).attr('class', 'tab-close ui-icon');
			$(divClose).attr('title', i18n.tab.closeTab);
			$(divClose).bind('click', {
						tabId : menuId,
						validateItem : validate
					}, TabTagUtils.closeTab);
			divNewTab.appendChild(divClose);
		}

		oDivTab.appendChild(divNewTab);
		
		var oDivFrame = $('#divFrame')[0];
		var oNewFrame = document.createElement('iframe');
		var jNewFrame = $(oNewFrame);
			jNewFrame.attr('id', menuId + '_frame');
			jNewFrame.attr('name', menuId + '_frame_name');
			jNewFrame.css('display', 'block');		
			jNewFrame.attr('scrolling','auto');
			jNewFrame.css('width', '100%');
			jNewFrame.css('height', '100%');		
			jNewFrame.css('border', '0');
			jNewFrame.css('frameborder', '0');		
		
		oDivFrame.appendChild(oNewFrame);

		// TODO 暂时解决
		if (menuURL.indexOf("?") != -1) {
			menuURL = menuURL + "&random=" + Math.random();
		} else {
			menuURL = menuURL + "?random=" + Math.random();
		}

		var event = {
			data : {
				tabId : menuId,
				src : menuURL
			}
		};

		TabTagUtils.showTab(event);
		
		OPENED_TABS_ID.push(menuId);
	},
	showTab : function(event) {
		var context = event.data;
		var _tabId = context.tabId;
		var _frameId = _tabId + '_frame';
		var _src = context.src;
		
		$('#' + ACTIVE_TAB_ID).attr('class', 'tab-title ui-state-default');
		$('#' + ACTIVE_TAB_ID + '_frame').css('display', 'none');
		
		$('#' + _tabId).attr('class', 'tab-title-up ui-state-active');
		$('#' + _frameId).css('display', 'block');
		
		ORIGINAL_TAB_ID = ACTIVE_TAB_ID;
		ACTIVE_TAB_ID = _tabId;
		if (_src) {
			$('#' + _frameId).attr('src', _src);
		}
		
	},
	closeTab : function(event) {
		var context = event.data;
		var _tabId = context.tabId;
		var _frameId = _tabId + '_frame';

		var currentTab = $('#' + _tabId);
		var currentFrame = $('#' + _frameId);
		try{
			if (TabTagUtils.isDirty(event.data.tabId)) {
				var bln = window.confirm(i18n.tab.msgDataChange);
				if (!bln) {
					return false;
				}
			};
			if(context.validateItem!=undefined){
				var valResult = eval(context.validateItem);
				if(valResult!=undefined&&!valResult){
					return false;
				};
			}
		}catch(e){}
		
		delete IS_DIRTY_FUNC[event.data.tabId];
		
		currentFrame[0].src = ''; //释放iframe资源
		
		currentTab[0].parentNode.removeChild(currentTab[0]);
		
		currentFrame[0].parentNode.removeChild(currentFrame[0]);
		
		OPENED_TABS_ID = OPENED_TABS_ID.del(OPENED_TABS_ID.indexOf(_tabId));
		
		if (_tabId == ACTIVE_TAB_ID) {
			var originalTabId = null;
			if (ORIGINAL_TAB_ID && OPENED_TABS_ID.contains(ORIGINAL_TAB_ID)) {
				originalTabId = ORIGINAL_TAB_ID;
			} else if (OPENED_TABS_ID.length > 0) {
				originalTabId = OPENED_TABS_ID[OPENED_TABS_ID.length - 1];
			}
			if (originalTabId) {
				var event = {
					data : {
						tabId : originalTabId
					}
				};
				TabTagUtils.showTab(event);
			}
		}
		
		if($.browser.msie){ ////手动回收内存
			setTimeout(function(){CollectGarbage();}, 200);
		}
	},
	closeActiveTab : function(){
		if(ACTIVE_TAB_ID){
			var event =  { data : { tabId : ACTIVE_TAB_ID } };
			TabTagUtils.closeTab(event);
		}
	},
	closeActiveTabById : function(_tabId){
		if(_tabId){
			if(OPENED_TABS_ID.indexOf(_tabId)>-1){
				var event =  { data : { tabId : _tabId } };
				TabTagUtils.closeTab(event);
			}
		}
	},
	closeAndNewTab : function(menuId, menuName, menuURL, closeAble, validate) {
		var b = TabTagUtils.closeActiveTabById(menuId);
		if(b == undefined || b === true){
			TabTagUtils.newTab(menuId, menuName, menuURL, closeAble, validate);
		}
	},
	setCwinHeight : function(id) {		
	},
	isDirty : function(currentTabId){
		// 占位符，勿动 GXL
		if(IS_DIRTY_FUNC[currentTabId]){
			return IS_DIRTY_FUNC[currentTabId]();
		}
		return false;
	},
	getOpenTabs : function(){
		return OPENED_TABS_ID;
	}
};